package com.niit.hadoop.maxandmin;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class MaxAndMinDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 0 根据自己电脑路径重新配置
        args = new String[]{"src/main/resources/mapreduce/maxandmin/input/MaxAndMin.txt", "src/main/resources/mapreduce/maxandmin/output"};

        // 1 获取配置信息，或者job对象实例
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 2 指定本程序的jar包所在的本地路径
        job.setJarByClass(MaxAndMinDriver.class);

        // 3 指定本业务job要使用的Mapper/Reducer业务类
        job.setMapperClass(MaxAndMinMapper.class);
        job.setReducerClass(MaxAndMinReducer.class);

        // 4 指定Mapper输出数据的kv类型
        job.setMapOutputKeyClass(MaxAndMinBean.class);
        job.setMapOutputValueClass(NullWritable.class);

        // 5 指定最终输出的数据的kv类型
        job.setOutputKeyClass(MaxAndMinBean.class);
        job.setOutputValueClass(NullWritable.class);

        // 6 指定输入和输出路径
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 7 提交Job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
